#导入请求模块 pip install requests
import requests
#内核模块无需按照
import re;
import json;
import pprint


url = 'https://www.acfun.cn/v/ac33606669'

#模拟浏览器请求
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'}

#1请求数据
reponse = requests.get(url=url,headers=headers)

#2获取数据
#print(reponse.text)


#3解析数据 <title >虎视眈眈～我盯上你了 - AcFun弹幕视频网 - 认真你就输啦 (?ω?)ノ- ( ゜- ゜)つロ</title>
title = re.findall("<title >(.*?) - AcFun弹幕视频网 - 认真你就输啦 \(\?ω\?\)ノ- \( ゜- ゜\)つロ</title>",reponse.text)

#print(title)
html_data=re.findall('window.videoInfo = (.*)',reponse.text)[0][:-1]

#print(html_data)
json_data=json.loads(html_data);

#字典取值
m3u8_url = json.loads(json_data['currentVideoInfo']['ksPlayJson'])['adaptationSet'][0]['representation'][0]['backupUrl'][0]

#获取m3u8数据
m3u8_data=requests.get(url=m3u8_url,headers=headers).text
#删掉 E 开头的
m3u8_data = re.sub('#E.*','',m3u8_data).split()
print(m3u8_data)

for link in m3u8_data:
    ts_url='https://ali-safety-video.acfun.cn/mediacloud/acfun/acfun_video/'+ link
    ts_name = link.split('.')[1]

    ts_contents=requests.get(url=ts_url,headers=headers).content
    with open('video\\'+ts_name+'.mp4',mode='wb') as f:
        f.write(ts_contents)
    print(ts_name,ts_url)